package com.hc.controller;

import com.hc.exception.*;
import com.hc.util.Constant;
import com.hc.vo.StatusVO;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

//@RestControllerAdvice("com.hc.controller")
public class ServiceExceptionAdvice {


    //找不到一律404
    @ExceptionHandler
    public StatusVO handleFieldNotFond(FieldNotExistException e) {
        return new StatusVO(404, e.getMessage());
    }

    @ExceptionHandler
    public StatusVO handleConstraintViolation(DataConstraintViolationException e) {
        return new StatusVO(400, e.getMessage());
    }


    @ExceptionHandler
    public StatusVO handleValidation(ConstraintViolationException e){
        return new StatusVO(400,e.getMessage());
    }

    @ExceptionHandler
    public StatusVO handle(WrongPasswordException e) {
        return new StatusVO(400, "用户名或密码错误");
    }

    @ExceptionHandler
    public StatusVO handleToken(IllegalTokenException e) {
        return new StatusVO(Constant.ILLEGAL_TOKEN_CODE, "错误的令牌");
    }

    @ExceptionHandler
    public StatusVO handleOther(OthersLoggedInException e) {
        return new StatusVO(50002, "其他用户已经登录");
    }
//    @ExceptionHandler
//    public ResponseEntity<StatusVO> handleValidation(MethodArgumentNotValidException e) {
//        e.printStackTrace();
//        System.out.println("error=========");
//        return ResponseEntity.status(50001).contentType(MediaType.APPLICATION_JSON).body(new StatusVO(400, e.getMessage()));
//    }


}
